package saucaca.fragment;

import java.util.ArrayList;

import saucaca.Object.SongObject;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.OnInitializedListener;
import com.google.android.youtube.player.YouTubePlayer.PlayerStyle;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerSupportFragment;
import com.lemon.kenhnhacvn.MainActivity;
import com.lemon.kenhnhacvn.R;

public class YoutubeFragment extends Fragment {
	private FragmentActivity myContext;
	String video_id="";
	public YouTubePlayer YPlayer;
	private static final String DeveloperKey = "AIzaSyAWgNluN3S8hMF_i18s8w7rOmXUBlK6tRM";

	
	public static YoutubeFragment newInstance(String videoid) {
		YoutubeFragment f = new YoutubeFragment();
		Bundle args = new Bundle();
		args.putString(MainActivity.INDEX, videoid);
		f.setArguments(args);
		return f;
	}
	
	
	@Override
	public void onAttach(Activity activity) {

		if (activity instanceof FragmentActivity) {
			myContext = (FragmentActivity) activity;
		}

		super.onAttach(activity);
	}

//	public YoutubeFragment(String videoid) {
//		this.video_id=videoid;
//	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.youtube_api_frame, container, false);
		Bundle arguments = getArguments();
		if (arguments != null && arguments.containsKey(MainActivity.INDEX))
			video_id = arguments.getString(MainActivity.INDEX);
		YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
		FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
		transaction.add(R.id.youtube_fragment, youTubePlayerFragment).commit();

		youTubePlayerFragment.initialize(DeveloperKey, new OnInitializedListener() {

			@Override
			public void onInitializationSuccess(Provider arg0, YouTubePlayer youTubePlayer, boolean b) {
				if (!b) {
					YPlayer = youTubePlayer;
					// YPlayer.setFullscreen(f);
					YPlayer.loadVideo(video_id);
//					YPlayer.setPlayerStyle(PlayerStyle.CHROMELESS);
					YPlayer.play();
				}
			}

			@Override
			public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) {
			}
		});
		return rootView;

	}
}
